package com.jiazhong.mingxing.boot.boot02.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.mingxing.boot.boot02.bean.Student;
import com.jiazhong.mingxing.boot.boot02.service.StudentService;
import com.jiazhong.mingxing.boot.boot02.util.EncryptMD5;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Slf4j
@SpringBootTest
public class App {
    @Resource
    private StudentService service;

    @Test
    public void a() {
        List<Student> list = service.list();
        log.info("list={}", list);
    }

    @Test
    public void b() {
        Student student = new Student();
        student.setName("王莹");
        student.setStuNo("RG2034626577");
        student.setGender('女');
        student.setBirthday("2003-1-6");
        student.setCourseId(1965250158253547036L);
        student.setSchoolId(1965321181514842113L);
        student.setPlaceOfOrigin("陕西宝鸡");
        student.setEnrollmentDate("2023-3-27");
        // 调用save方法
        boolean b = service.save(student);

        log.info("b={}", b);
    }

    @Test
    public void c() {
        boolean b = service.removeById(1966791867963351041L);
        log.info("b={}", b);
    }

    @Test
    public void d() {
        Student student = new Student();
        student.setId(1966791867963351041L);
        student.setBirthday("2003-9-4");

        service.updateById(student);
    }

    @Test
    public void e1() {
        int page = 1; // 页码
        int size = 3; // 每页显示条数
        // 产生了分页对象
        IPage<Student> iPage = Page.of(page, size);
        List<Student> list = service.list(iPage);
        iPage.setRecords(list);
        list.forEach(e -> log.info(":{}", e));
        log.info("iPage={}", iPage);
    }

    @Test
    public void e2() {
        int page = 1;
        int size = 2;
        PageHelper.startPage(page, size);
        List<Student> list = service.list();
        list.forEach(e -> log.info(":{}", e));
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        log.info("pageInfo={}", pageInfo);
    }

    @Test
    public void e3() {
        PageHelper.startPage(1, 2);
        // 全查询 引入分页后将全查询作出了修改
        List<Student> list = service.list();
        // 分页封装类，包含了：当前页的数据,zong条数,总页数,当前页等
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        // 当前页的数据
        List<Student> nowPageData = pageInfo.getList();
        // 总条数
        long total = pageInfo.getTotal();
        // 总页数
        int pages = pageInfo.getPages();
        // 当前页的条数
        int pageSize = pageInfo.getPageSize();
        // 当前页数
        int pageNum = pageInfo.getPageNum();
        log.info("total={}", total);
        log.info("pages={}", pages);
        log.info("pageSize={}", pageSize);
        log.info("pageNum={}", pageNum);
        log.info("nowPageData={}", nowPageData);
    }

    @Test
    public void f1() {
        // 构建条件
        // 1. 条件构造器 :查询、修改
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        // 2. 设置条件
        queryWrapper.eq("gender", "女"); // eq:=
        // 3. 使用
        List<Student> list = service.list(queryWrapper);
    }

    @Test
    public void f2() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("birthday", "2003-1-1");
        queryWrapper.le("birthday", "2003-1-31");
        List<Student> list = service.list(queryWrapper);
    }

    @Test
    public void f3() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "王");
        List<Student> list = service.list(queryWrapper);
    }

    @Test
    public void f4() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("place_of_origin", "西安")
                .or()
                .eq("gender", "男");
        List<Student> list = service.list(queryWrapper);
    }

    @Test
    public void f5() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        // 只查询部分列
        queryWrapper.select("id", "name", "birthday", "school_id", "place_of_origin");
        List<Student> list = service.list(queryWrapper);
    }

    @Test
    public void g1() {
        // 1. 条件构造器
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        // 2. 设置更新的列和值
        updateWrapper.set("place_of_origin", "陕西咸阳");
        // 3. 设置更新条件
        updateWrapper.eq("name", "王莹");
        // 4. 执行操作
        boolean b = service.update(updateWrapper);
    }

    // 1.如何向表中录入数据
    // 2.如何修改表中的数据（按照ID修改｜UpdateWrapper）
    // 3.如何删除表中的数据（逻辑删除）
    // 4.如何全查询
    // 5.如何按照ID获取一条数据
    // 6.如何检索（QueryWrapper）
    // 7.如何分页（自带｜pageHelper）
}
